package com.yztc.love_travel.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yztc.love_travel.ui.main.MainActivity;
import com.yztc.love_travel.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.imageview_name)
    ImageView imageviewName;
    @BindView(R.id.edit_psw)
    EditText editPsw;
    @BindView(R.id.imageview_psw)
    ImageView imageviewPsw;
    @BindView(R.id.textview_register)
    TextView textviewRegister;
    @BindView(R.id.textview_forget_psw)
    TextView textviewForgetPsw;
    @BindView(R.id.activity_login)
    LinearLayout activityLogin;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initToolBar();
        initView();
    }

    private void initView() {
        editName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String name = editName.getEditableText() + "";
                if(!TextUtils.isEmpty(name)){
                    imageviewName.setVisibility(View.VISIBLE);
                }else{
                    imageviewName.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editPsw.addTextChangedListener(new TextWatcher() {//当没有输入内容的时候，清空按钮不可见
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String psw = editPsw.getEditableText() + "";
                if(!TextUtils.isEmpty(psw)){
                    imageviewPsw.setVisibility(View.VISIBLE);
                }else{
                    imageviewPsw.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initToolBar() {
        toolbar.setTitle(R.string.login);
        toolbar.setNavigationIcon(R.drawable.ic_back);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.edit_name, R.id.imageview_name, R.id.edit_psw, R.id.imageview_psw, R.id.textview_register, R.id.textview_forget_psw, R.id.activity_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.edit_name:
                break;
            case R.id.imageview_name:
                editName.setText("");
                break;
            case R.id.edit_psw:

                break;
            case R.id.imageview_psw:
                editPsw.setText("");
                break;
            case R.id.textview_register:
                break;
            case R.id.textview_forget_psw:
                break;
            case R.id.activity_login:
                Intent intent = new Intent();
                intent.setClass(this, MainActivity.class);
                startActivity(intent);
                break;
        }
    }
}
